package GradeManager.StudentGradeSystem.utils;

import GradeManager.StudentGradeSystem.dao.StudentDao;
import GradeManager.StudentGradeSystem.pojo.Student;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UpdateStudentGrade {
    private Student updatedSubject;
    @Autowired
    private StudentDao studentDao;
    public void setUpdatedSubject(Student updatedSubject){
        this.updatedSubject=updatedSubject;
    }
    public void update(){
//            根据学年、学号、科目来查询原来此科目的成绩
        LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Student::getSchoolYear,updatedSubject.getSchoolYear())
                .eq(Student::getStudentId,updatedSubject.getStudentId())
                .eq(Student::getSubjectName,updatedSubject.getSubjectName());
        Student rivalSubject = studentDao.selectOne(lqw);
        if(updatedSubject.getCourseMark()==null && rivalSubject!=null){
//              如果修改后的成绩为空，且原来的科目成绩存在，则删除该条成绩记录
                studentDao.delete(lqw);
        }else if(updatedSubject.getCourseMark()!=null && rivalSubject==null){
//            如果修改后的成绩不为空,，且原来的科目成绩不存在，则新增该条成绩记录
            studentDao.insert(updatedSubject);
        }else if(updatedSubject.getCourseMark()==null && rivalSubject==null){
        }else {
            //除了以上情况外
//            如果分数没有修改，则直接返回
            if(rivalSubject.getCourseMark().equals(updatedSubject.getCourseMark()) ){
                return;
            }
//            修改学生单科成绩
            studentDao.updateGrade(updatedSubject);
        }
    }
}
